package park.borne;

/**
 *
 * <p>Titre : Un petit tampon</p>
 * <p>Description : tampon circulaire utilisé pour gérer les événements.
 * Chaque événement est codé sur un octet</p>
 * <p>Copyright : Copyright (c) 2003</p>
 * <p>Société : </p>
 * @author Gilles Petot
 * @version 1.0
 */
public final class TinyEventQueue
{
   private static final int TAILLETAMPON = 50;
   private byte[] tampon = new byte[TAILLETAMPON];
   private int pointeurTamponIn = 0;
   private int pointeurTamponOut = 0;

   public final void write(byte ev)
   {
	int pointeurTemp;

	pointeurTemp = pointeurTamponIn;
	if (pointeurTemp < (TAILLETAMPON-1)) pointeurTemp++; else pointeurTemp = 0;
	if (pointeurTemp != pointeurTamponOut)  //detection tampon plein
	{
	   tampon[pointeurTamponIn] = (byte)ev;
	   pointeurTamponIn = pointeurTemp;
	}
   }

   public final byte read()
   {
	byte result = -1;
	{
	  if (pointeurTamponOut != pointeurTamponIn) //detection tampon vide
	  {
	     result = (byte)tampon[pointeurTamponOut];
	     if (pointeurTamponOut < (TAILLETAMPON -1))  pointeurTamponOut++; else pointeurTamponOut = 0;
	  }
       }
       return (byte)result;
   }

}